import { request } from './request'

const api = {
  upsert: '/commonInstance/upsert',
  upsertNoRecover: '/commonInstance/upsertNoRecover',
  detail: '/commonInstance/detail',
  pageList: '/commonInstance/findList',
  pageListAll: '/commonInstance/findListAll',
  delete: '/commonInstance/delete',
  aggregate: '/commonInstance/aggregate',
  detailOne: '/commonInstance/detailOne',
}

// 增加编辑
export interface IUpsertParams {
  _id?: string
  id?: string
  data: Record<string, any> // ILand| IBuilding | IWorkshop | IDepository | IShop
  templateName: string
}

export const upsert = (params: IUpsertParams) => {
  return request.put(api.upsert, params)
}

export const upsertNoRecover = (params: IUpsertParams) => {
  return request.put(api.upsertNoRecover, params)
}

// 详情
export interface IDetailParams {
  id: string | number
  templateName: string
}

export const detail = (params: IDetailParams) => {
  return request.get(`${api.detail}/${params.templateName}/${params.id}`)
}

export interface IDetailOneParams {
  templateName: string
  conditions: Record<string, any>
}

export const detailOne = (params: IDetailOneParams) => {
  return request.post(`${api.detailOne}/${params.templateName}`, params.conditions)
}

export interface IDelParams extends IDetailParams {}

export const del = (params: IDelParams) => {
  return request.delete(`${api.delete}/${params.templateName}/${params.id}`)
}

// 分页列表

export interface IDicCode {
  name: string
  type: number
  typeKey: string
}

export interface IPageInfo {
  pageNum: number
  pageSize: number
}
export interface IPageListParams {
  conditions?: Record<string, unknown>
  dicCodes?: IDicCode[]
  fields?: Record<string, unknown>
  pageInfo?: IPageInfo
  sort?: Record<string, unknown>
  templateName: string
}

export const pageList = (params: IPageListParams) => {
  return request.post(api.pageList, params)
}

export const pageListAll = (params: IPageListParams) => {
  return request.post(api.pageListAll, params)
}

// 聚合查询

export interface IAggregateParams {
  cmdList: Record<string, unknown>[]
  templateName: string
}

export const aggregate = (params: IAggregateParams) => {
  return request.post(api.aggregate, params)
}
